import Vue from 'vue'
import Router from 'vue-router'

// 引入多个模块的路由规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

// 动态路由
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

Vue.use(Router)

import Layout from '@/layout'
// 静态路由
export const constantRoutes = [
  {
    path: '/test01',
    component: () => import('@/views/test')
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  {
    path: '/import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [
      {
        path: '', // 二级路由path什么都不写 表示二级默认路由
        component: () => import('@/views/import/index.vue')
      }
    ]
  }
]

const createRouter = () =>
  new Router({
    base: 'hr',
    mode: 'history', // 路由模式分hash模式和history模式 hash模式：浏览器访问地址有#,相当于http://localhost:1874/#/dashboard
    // hash模式特点：前端访问页面，地址变化不会重新刷新页面  history模式：路径用/ 配置base属性相当于在路径前加个前缀
    //* *hash模式** ： #后面是路由路径，特点是前端访问，#后面的变化不会经过服务器
    // **history模式**：正常的/访问模式，特点是后端访问，任意地址的变化都会访问服务器
    // > 开发到现在，我们一直都在用hash模式，打包我们尝试用history模式
    scrollBehavior: () => ({ y: 0 }),
    // routes: [...constantRoutes, ...asyncRoutes] // 动态路由和静态路由的合并 数组合并 用延展运算符
    routes: [...constantRoutes] // 动态路由和静态路由解除合并
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
